package com.sgcc.sx.pmos.e_sx_mall.fragment;

import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.XPopup;
import com.sgcc.sx.pmos.common.base.MvpBaseFragment;
import com.sgcc.sx.pmos.common.bean.EventCenter;
import com.sgcc.sx.pmos.common.bean.PowMonth;
import com.sgcc.sx.pmos.common.dialog.PowaBnormalPopView;
import com.sgcc.sx.pmos.common.utils.ArguConstant;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.sx.pmos.e_sx_mall.adapter.PowerSetTotalAdapter;
import com.sgcc.sx.pmos.e_sx_mall.bean.DataSelect;
import com.sgcc.sx.pmos.e_sx_mall.contract.PowSetTotalContract;
import com.sgcc.sx.pmos.e_sx_mall.presenter.PowSetTotalPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品
 */
public class PowSetTotalFragment extends MvpBaseFragment<PowSetTotalPresenter> implements PowSetTotalContract.View {

    private RecyclerView mRecycleview;
    public BaseQuickAdapter adapter;
    public PowerSetTotalAdapter powerSetTotalAdapter;
    TextView next_txt;

    public List<PowMonth> mData = new ArrayList<>();

    public boolean isrefresh = false;//是否是刷新状态
    int startMonth;
    int endMonth;
    String year;

    @Override
    public void querySuccess() {

    }

    @Override
    public void queryFailed() {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_powersettotal;
    }

    @Override
    protected PowSetTotalPresenter createPresenter() {
        return new PowSetTotalPresenter();
    }

    @Override
    protected void initViews() {
        super.initViews();
        findViewByIds();
    }

    private void findViewByIds() {
        mRecycleview = mRootView.findViewById(R.id.recycleview);
        next_txt = mRootView.findViewById(R.id.next_txt);
        next_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!mPresenter.checkPowList(mData)) {
                    String tipickContent=mPresenter.getTotalTipick(mData);
                    if(TextUtils.isEmpty(tipickContent)){
                        new XPopup.Builder(mContext)
                                .isDestroyOnDismiss(false)
                                .isViewMode(true)
                                .autoOpenSoftInput(false)
                                .asCustom(new PowaBnormalPopView(getContext(), new PowaBnormalPopView.OnListener() {
                                    @Override
                                    public void onPositiveClick(PowaBnormalPopView pop, boolean ischeck) {
                                        pop.dismiss();
                                        // 立即下单
                                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.PLACEORDER_GO_CONTRACT));
                                    }
                                }, "友情提示", tipickContent, "", "继续下单", false,false,true))
                                .show();
                    }else{
                        EventBus.getDefault().post(new EventCenter<>(ArguConstant.PLACEORDER_GO_CONTRACT));
                    }
                }
            }
        });
        powerSetTotalAdapter = new PowerSetTotalAdapter();
        powerSetTotalAdapter.setItemClickListener(new PowerSetTotalAdapter.OnItemClickListener() {
            @Override
            public void OnItemsClickListener(int position, String s) {

            }
        });




//        getDataRequest( );
//        loadMore();

    }

    /**
     * @author yinsh
     * @description 请求成功
     * @date 2021/8/19 上午10:53
     */
    public void loadSuccess(boolean hasNext, List<PowMonth> datas) {
        if (isrefresh) {
            mData.clear();
        }
        if (datas != null) {
            mData.addAll(datas);
        }
        powerSetTotalAdapter.setYear(year);
        powerSetTotalAdapter.setNewData(mData);
        mRecycleview.setLayoutManager(new GridLayoutManager(getContext(), 1));
        mRecycleview.setAdapter(powerSetTotalAdapter);
        powerSetTotalAdapter.notifyDataSetChanged();
    }


    public List<PowMonth> getPowMonthList() {
        return mData;
    }

    private void getDataRequest( ) {

        List<PowMonth> list = new ArrayList<>();
        for (int i = startMonth; i < endMonth + 1; i++) {
            PowMonth powMont = new PowMonth();
            powMont.setMonthDate(i);
            list.add(powMont);
        }
        loadSuccess(false, list);
    }

    public void setSelectData(DataSelect dataSelect) {
        LogUtils.v(dataSelect.getStartdata() + "------" + dataSelect.getEnddata());
         startMonth = Integer.valueOf(dataSelect.getStartdata().substring(5, 7));
         endMonth = Integer.valueOf(dataSelect.getEnddata().substring(5, 7));
        year = dataSelect.getStartdata().substring(0, 4);
        getDataRequest();


    }
}
